# Maintainer: Martell Malone <martellmalone@gmail.com>

_realname=cocos2d-x
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
pkgver=3.14.1
_deps_ver=130
pkgrel=1
pkgdesc="Open source game framework written in C++ (mingw-w64)"
arch=('any')
url="http://www.cocos2d-x.org/"
license=("MIT")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "make" "${MINGW_PACKAGE_PREFIX}-cmake" "git")
depends=("${MINGW_PACKAGE_PREFIX}-box2d"
         "${MINGW_PACKAGE_PREFIX}-bullet"
         "${MINGW_PACKAGE_PREFIX}-chipmunk>7.0.0"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-flatbuffers"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-glfw"
         "${MINGW_PACKAGE_PREFIX}-glew"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libvorbis"
         "${MINGW_PACKAGE_PREFIX}-libwebsockets"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-mpg123"
         "${MINGW_PACKAGE_PREFIX}-openal"
         "${MINGW_PACKAGE_PREFIX}-protobuf"
         "${MINGW_PACKAGE_PREFIX}-tinyxml2"
         "${MINGW_PACKAGE_PREFIX}-xxhash"
         "${MINGW_PACKAGE_PREFIX}-zlib")

options=('staticlibs' 'strip')
source=(https://github.com/cocos2d/cocos2d-x/archive/${_realname}-${pkgver}.tar.gz
        "https://github.com/cocos2d/cocos2d-x-3rd-party-libs-bin/archive/v3-deps-${_deps_ver}.tar.gz"
        001-bullet-find.patch)
sha256sums=('d764a982dbb2bc702e2456306d90d990435975defeb2a158c05d09d9e9c76cb7'
            '689b4fb6601fa0a3947bd342e5b767b59a44256b9d89130ef622b8d9bdf3cec0'
            '37ef1d44ae4a01e943f84a0c56efe8bca54a6c22d31a2f34168eb9c472361580')

prepare() {
  cd "${srcdir}"
  cp -r ${srcdir}/cocos2d-x-3rd-party-libs-bin-3-deps-${_deps_ver}/. ${srcdir}/${_realname}-${_realname}-${pkgver}/external

  cd "${srcdir}/${_realname}-${_realname}-${pkgver}"
  patch -p1 -i ${srcdir}/001-bullet-find.patch
  #force depend on mingw-tinyxml2
  rm -rf external/tinyxml2
  rm -rf external/unzip
}

build() {
  mkdir -p "${srcdir}/build-${CARCH}"
  cd "${srcdir}/build-${CARCH}"

  unset LDFLAGS CFLAGS CXXFLAGS

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DDEBUG_MODE=OFF \
    -DBUILD_LUA_LIBS=OFF \
    -DBUILD_LUA_TESTS=OFF \
    -DBUILD_JS_LIBS=OFF \
    -DBUILD_JS_TESTS=OFF \
    -DBUILD_CPP_TESTS=OFF \
    -DBUILD_EDITOR_COCOSTUDIO=OFF \
    -DBUILD_EDITOR_COCOSBUILDER=OFF \
    -DBUILD_EDITOR_SPINE=OFF \
    -DUSE_RECAST=OFF \
    -DUSE_PREBUILT_LIBS=OFF \
    ../${_realname}-${_realname}-${pkgver}

  make
}

package() {
  mkdir -p "${pkgdir}${MINGW_PREFIX}/include/cocos"
  mkdir -p "${pkgdir}${MINGW_PREFIX}/lib"
  cd "${srcdir}/${_realname}/cocos"
  cp -r ${srcdir}/build-${CARCH}/lib/. ${pkgdir}${MINGW_PREFIX}/lib

  #little helper because we don't want to pollute include with all the sub dirs ;)
  #echo '#include "cocos/cocos2d.h"' > ${pkgdir}${MINGW_PREFIX}/include/cocos2d.h
  #looks like we will pollute until the cocos devs change include structure #cocos/

  find -type f \( -name "*.h*" -o -name "*.inl" \) -exec install -D {} ${pkgdir}${MINGW_PREFIX}/include/{} \;

  #put win32 into include because we are on windows
  cd "${pkgdir}${MINGW_PREFIX}/include"
  mv platform/win32/* .
  sed -i "s/platform\/win32\///g" cocos2d.h

  # then get all the dirs but parent
  find platform -mindepth 1 -depth -type d -not -wholename platform/desktop -prune -exec rm -rf {} +

  # add extensions
  mkdir -p "${pkgdir}${MINGW_PREFIX}/include/extensions"
  cd "${srcdir}/${_realname}/extensions"
  find -type f \( -name "*.h*" -o -name "*.inl" \) -exec install -D {} ${pkgdir}${MINGW_PREFIX}/include/extensions/{} \;

  # install templates for cocos2d-console
  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/cocos2d-x"
  cp -r ${srcdir}/$_realname/templates/ ${pkgdir}${MINGW_PREFIX}/share/cocos2d-x
}
